//
//  FZQTabBar.swift
//  FZQSinLangWeiBo
//
//  Created by fuzhiqiang on 17/1/17.
//  Copyright © 2017年 fzq. All rights reserved.
//

import UIKit

class FZQTabBar: UITabBar {
//1.声明一个闭包
    var colsure: (()->())?
    override init(frame: CGRect){
        super.init(frame: frame)
        setupUI()
    }
    //只能纯代码开发不能用xib与sb
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    private func setupUI(){
        addSubview(composeButton)
    }
    override func layoutSubviews() {
        
            super.layoutSubviews()
        //计算UITabBarButton宽度
        let w = FZQSCRENW * 0.2
        //确定每个UITabBarButton的位置
        var index: CGFloat = 0
        //遍历UITabBarButton的子控件
        for view in subviews {
            // 判断class 是否是UITabBarButton
            if view.isKind(of: NSClassFromString("UITabBarButton")!) {
                // 设置UITabBarButton 的 宽度 和 x
                view.frame.size.width = w
                view.frame.origin.x = index * w
                index += 1
                //给加号预留位置
                if index == 2 {
                    index += 1
                }
            }
        }
        //设置加号按钮的frame
        composeButton.center.x = frame.width * 0.5
        composeButton.center.y = frame.height * 0.5
    }
  //懒加载加号控件
    private lazy var composeButton: UIButton = {
        let btn = UIButton()
        //添加监听方法
        btn.addTarget(self, action: #selector(buttonClick), for: UIControlEvents.touchUpInside)
        //设置image
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), for: UIControlState.normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), for: UIControlState.highlighted)
        //设置背景图片
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), for: UIControlState.normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), for: UIControlState.highlighted)
        //设置按钮大小，不然无法显示
        btn.sizeToFit()
        return btn;
    }()
    //按钮点击方法
   @objc private func buttonClick(){
        print("点了一下")
    //3.执行闭包
    colsure?()
    }
}
